#include "frmTerrain.h"
#include "GameSystemManager.h"
#include "TerrainSystem.h"
#include "GUISystem.h"

//#include "GameSystemManager.h"

frmTerrain::frmTerrain() {
	subscribeEvents();
	setup();
}
frmTerrain::~frmTerrain() {

}
	
void frmTerrain::subscribeEvents() {
	////////////////////////////////////////////////////////////////////////////////////////////////////
	//Subsribe GUI Terrain events
	////////////////////////////////////////////////////////////////////////////////////////////////////

	//unused
	//CEGUI::Window* btnTerrainNew = CEGUI::WindowManager::getSingleton().getWindow("Terrain/btnNew");
	//btnTerrainNew->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmTerrain::evtTerrainNew_click,this));

	//Destroy terrain
	CEGUI::Window* btnTerrainDelete = CEGUI::WindowManager::getSingleton().getWindow("Terrain/btnDelete");
	btnTerrainDelete->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmTerrain::evtTerrainDelete_click,this));

	//unused
	//CEGUI::Window* btnTerrainRefresh = CEGUI::WindowManager::getSingleton().getWindow("Terrain/btnRefresh");
	//btnTerrainRefresh->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmTerrain::evtTerrainRefresh_click,this));

	//Generate terrain
	CEGUI::Window* btnTerrainGenerate = CEGUI::WindowManager::getSingleton().getWindow("Terrain/btnGenerate");
	btnTerrainGenerate->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmTerrain::evtTerrainGenerate_click,this));

	//Terrain heightmap
	CEGUI::Window* btnTerrainList = CEGUI::WindowManager::getSingleton().getWindow("Terrain/List");
	btnTerrainList->subscribeEvent(CEGUI::Listbox::EventSelectionChanged,CEGUI::Event::Subscriber(&frmTerrain::evtTerrainList_click,this));

	//Terrain brush size
	CEGUI::Window* sldTerrainBrushSize = CEGUI::WindowManager::getSingleton().getWindow("Terrain/brushTerrainSizeSlider");
	sldTerrainBrushSize->subscribeEvent(CEGUI::Slider::EventValueChanged,CEGUI::Event::Subscriber(&frmTerrain::evtTerrainBrushSize_change,this));

	//Terrain brush intensity
	CEGUI::Window* sldTerrainBrushIntensity = CEGUI::WindowManager::getSingleton().getWindow("Terrain/brushTerrainIntensitySlider");
	sldTerrainBrushIntensity->subscribeEvent(CEGUI::Slider::EventValueChanged,CEGUI::Event::Subscriber(&frmTerrain::evtTerrainBrushIntensity_change,this));

	//Paint brush size
	CEGUI::Window* sldTerrainPaintSize = CEGUI::WindowManager::getSingleton().getWindow("Terrain/brushPaintSizeSlider");
	sldTerrainPaintSize->subscribeEvent(CEGUI::Slider::EventValueChanged,CEGUI::Event::Subscriber(&frmTerrain::evtTerrainPaintSize_change,this));

	//Paint brush intensity
	CEGUI::Window* sldTerrainPaintIntensity = CEGUI::WindowManager::getSingleton().getWindow("Terrain/brushPaintIntensitySlider");
	sldTerrainPaintIntensity->subscribeEvent(CEGUI::Slider::EventValueChanged,CEGUI::Event::Subscriber(&frmTerrain::evtTerrainPaintIntensity_change,this));

	//Current palette of textures
	CEGUI::Window* lstTerrainTextureList = CEGUI::WindowManager::getSingleton().getWindow("Terrain/lstTextures");
	lstTerrainTextureList->subscribeEvent(CEGUI::Listbox::EventSelectionChanged,CEGUI::Event::Subscriber(&frmTerrain::evtTerrainTextureList_click,this));

	//Selection of textures found on disk
	CEGUI::Window* lstTexturesFoundList = CEGUI::WindowManager::getSingleton().getWindow("Terrain/lstTexturesDisk");
	lstTexturesFoundList->subscribeEvent(CEGUI::Listbox::EventSelectionChanged,CEGUI::Event::Subscriber(&frmTerrain::evtTerrainTexturesDiskList_click,this));

	//Remove texture from palette
	CEGUI::Window* btnTerrainRemoveFromPalette = CEGUI::WindowManager::getSingleton().getWindow("Terrain/btnRemovePaletteItem");
	btnTerrainRemoveFromPalette->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmTerrain::evtTerrainRemoveFromPalette_click,this));

	//Add texture to palette
	CEGUI::Window* btnTerrainAddToPalette = CEGUI::WindowManager::getSingleton().getWindow("Terrain/btnAddTexture");
	btnTerrainAddToPalette->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmTerrain::evtTerrainAddToPalette_click,this));
	

	//Window close button
	CEGUI::Window* btnTerrainClose = CEGUI::WindowManager::getSingleton().getWindow("Terrain");
	btnTerrainClose->subscribeEvent(CEGUI::FrameWindow::EventCloseClicked,CEGUI::Event::Subscriber(&frmTerrain::evtTerrainClose_click,this));
	
}

void frmTerrain::fillHeightmapList() {
	//Show available heightmaps
	CEGUI::Listbox* lbox = (CEGUI::Listbox*) CEGUI::WindowManager::getSingleton().getWindow("Terrain/List");
	lbox->resetList();
	StringVectorPtr resourceNames = ResourceGroupManager::getSingleton().findResourceNames("ET","ETterrain*.png" );
	std::vector<Ogre::String>::const_iterator itResourceName = resourceNames->begin();
	while ( itResourceName != resourceNames->end() )
	{
		CEGUI::ListboxTextItem* item = new CEGUI::ListboxTextItem(*itResourceName);
		lbox->addItem(item);
		++itResourceName;
	} 
}

void frmTerrain::fillTexturesList() {
	//Show splatting textures
	CEGUI::Listbox* lbox = (CEGUI::Listbox*) CEGUI::WindowManager::getSingleton().getWindow("Terrain/lstTextures");
	lbox->resetList();
	StringVectorPtr resourceNames = ResourceGroupManager::getSingleton().findResourceNames("ET","splatting*.png" );
	std::vector<Ogre::String>::const_iterator itResourceName = resourceNames->begin();
	while ( itResourceName != resourceNames->end() )
	{
		CEGUI::ListboxTextItem* item = new CEGUI::ListboxTextItem(*itResourceName);
		lbox->addItem(item);
		++itResourceName;
	} 
}

void frmTerrain::fillTexturesFoundList() {
	//Show founded splatting textures
	CEGUI::Listbox* lbox = (CEGUI::Listbox*) CEGUI::WindowManager::getSingleton().getWindow("Terrain/lstTexturesDisk");
	lbox->resetList();
	StringVectorPtr resourceNames = ResourceGroupManager::getSingleton().findResourceNames("ET","*.png" );
	std::vector<Ogre::String>::const_iterator itResourceName = resourceNames->begin();
	while ( itResourceName != resourceNames->end() )
	{
		CEGUI::ListboxTextItem* item = new CEGUI::ListboxTextItem(*itResourceName);
		lbox->addItem(item);
		++itResourceName;
	} 
}

void frmTerrain::setup() {
	
	//GameSystemManager::getSingleton()->getTerrain()->mPointer
	

	fillHeightmapList();
	fillTexturesList();
	fillTexturesFoundList();

	CEGUI::Window* w = CEGUI::WindowManager::getSingleton().getWindow("Terrain");
	w->setVisible(true);

	//Terrain brush size
	CEGUI::Slider* sld = (CEGUI::Slider*) CEGUI::WindowManager::getSingleton().getWindow("Terrain/brushTerrainSizeSlider");
	sld->setMaxValue(45);
	sld->setCurrentValue(16);
	CEGUI::Window *lbl = (CEGUI::Window*)CEGUI::WindowManager::getSingleton().getWindow("Terrain/brushTerrainSizeValue");
	lbl->setText("16");

	//Terrain intensity brush
	sld = (CEGUI::Slider*) CEGUI::WindowManager::getSingleton().getWindow("Terrain/brushTerrainIntensitySlider");
	sld->setMaxValue(5);
	sld->setCurrentValue(0.4);
	lbl = (CEGUI::Window*)CEGUI::WindowManager::getSingleton().getWindow("Terrain/brushTerrainIntensityValue");
	lbl->setText("0.4");
	//GameSystemManager::getSingleton()->getTerrain()->mTerrainBrushIntensity = 0.4f;
	terrain_config.mTerrainBrushIntensity = 0.4f;
	//GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("TerrainBrushIntensity")->setValue("0.4f");

	

	//terrain_config.terrainBrushIntensity = 0.4f;

	//Painting brush size
	sld = (CEGUI::Slider*) CEGUI::WindowManager::getSingleton().getWindow("Terrain/brushPaintSizeSlider");
	sld->setMaxValue(45);
	sld->setCurrentValue(14);
	lbl = (CEGUI::Window*)CEGUI::WindowManager::getSingleton().getWindow("Terrain/brushPaintSizeValue");
	lbl->setText("14");

	//Painting intensity brush
	sld = (CEGUI::Slider*) CEGUI::WindowManager::getSingleton().getWindow("Terrain/brushPaintIntensitySlider");
	sld->setMaxValue(5);
	sld->setCurrentValue(2);
	lbl = (CEGUI::Window*)CEGUI::WindowManager::getSingleton().getWindow("Terrain/brushPaintIntensityValue");
	lbl->setText("2");
	//terrain_config.paintBrushIntensity = 2;
	
	terrain_config.mPaintBrushIntensity = 2;
	//GameSystemManager::getSingleton()->getTerrain()->mPaintBrushIntensity = 2;
	//GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("PaintBrushIntensity")->setValue("2");

}

bool frmTerrain::fillConfigFromGui() {
	/*
	GameSystemParameter *mSizeX = GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("TerrainSizeX");
	GameSystemParameter *mSizeY = GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("TerrainSizeY");
	GameSystemParameter *mSizeZ = GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("TerrainSizeZ");
	GameSystemParameter *mLODMorphF = GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("LODMorphFactor");
	GameSystemParameter *mLODMorph = GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("LODMorphing");
	GameSystemParameter *mLODErrMargin = GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("LODErrorMargin");
	GameSystemParameter *mMaxTextures = GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("MaxSplattingTextures");
	GameSystemParameter *mCurrentTextures = GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("CurrentSplattingTextures");
	GameSystemParameter *mHeightMapName = GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("HeightmapFileName")->setValue();
	*/
	
	/*
	std::string Terr_SizeX = GameSystemManager::getSingleton()->getGUI()->getElement("Terrain/txtLongX")->getText().c_str();
	std::string Terr_SizeY = GameSystemManager::getSingleton()->getGUI()->getElement("Terrain/txtLongY")->getText().c_str();
	std::string Terr_SizeZ = GameSystemManager::getSingleton()->getGUI()->getElement("Terrain/txtLongZ")->getText().c_str();
	std::string Terr_LODMFact = GameSystemManager::getSingleton()->getGUI()->getElement("Terrain/txtLODMorphFactor")->getText().c_str();
	std::string Terr_LODM = GameSystemManager::getSingleton()->getGUI()->getElement("Terrain/chkUseLODMorphing")->getText().c_str();
	std::string Terr_LODErr = GameSystemManager::getSingleton()->getGUI()->getElement("Terrain/txtLODErrorMargin")->getText().c_str();
	*/

	terrain_config.mTerrainSizeX = GameSystemManager::getSingleton()->getGUI()->EditBoxToFloat("Terrain/txtLongX");
	terrain_config.mTerrainSizeY = GameSystemManager::getSingleton()->getGUI()->EditBoxToFloat("Terrain/txtLongY");
	terrain_config.mTerrainSizeZ = GameSystemManager::getSingleton()->getGUI()->EditBoxToFloat("Terrain/txtLongZ");
	terrain_config.mLODMorphFactor = GameSystemManager::getSingleton()->getGUI()->EditBoxToFloat("Terrain/txtLODMorphFactor");
	terrain_config.mLODMorphing = GameSystemManager::getSingleton()->getGUI()->CheckBoxToBool("Terrain/chkUseLODMorphing");
	terrain_config.mLODErrorMargin = GameSystemManager::getSingleton()->getGUI()->EditBoxToFloat("Terrain/txtLODErrorMargin");
	terrain_config.mMaxSplattingTextures = 6;
	terrain_config.mCurrentSplattingTextures = 0;
	terrain_config.mHeightmapFileName = GameSystemManager::getSingleton()->getGUI()->EditBoxToChr("Terrain/txtSelected");



	/*
	GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("TerrainSizeX")->setValue(Terr_SizeX);
	GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("TerrainSizeY")->setValue(Terr_SizeY);
	GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("TerrainSizeZ")->setValue(Terr_SizeZ);
	GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("LODMorphFactor")->setValue(Terr_LODMFact);
	GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("LODMorphing")->setValue(Terr_LODM);
	GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("LODErrorMargin")->setValue(Terr_LODErr);
	GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("MaxSplattingTextures")->setValue("6");
	GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("CurrentSplattingTextures")->setValue("0");
	GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("HeightmapFileName")->setValue("");
	*/

/*
	mSizeX->setValue(GameSystemManager::getSingleton()->getGUI()->EditBoxToFloat("Terrain/txtLongX"));
	mSizeY->setValue(GameSystemManager::getSingleton()->getGUI()->EditBoxToFloat("Terrain/txtLongY"));
	mSizeZ->setValue(GameSystemManager::getSingleton()->getGUI()->EditBoxToFloat("Terrain/txtLongZ"));
	mLODMorphF->setValue(GameSystemManager::getSingleton()->getGUI()->EditBoxToFloat("Terrain/txtLODMorphFactor"));
	mLODMorph->setValue(GameSystemManager::getSingleton()->getGUI()->CheckBoxToBool("Terrain/chkUseLODMorphing"));
	mLODErrMargin->setValue(GameSystemManager::getSingleton()->getGUI()->EditBoxToFloat("Terrain/txtLODErrorMargin"));
	mMaxTextures->setValue(6);
	mCurrentTextures->setValue(0);

	if(mHeightMapName->toString() == "")
		return false;
*/

	return true;
	
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
//Terrain GUI Events
bool frmTerrain::evtTerrainNew_click(const CEGUI::EventArgs& e) {
	return true;
}
bool frmTerrain::evtTerrainDelete_click(const CEGUI::EventArgs& e) {
	GameSystemManager::getSingleton()->getGUI()->showLoader(true);
	
	GameSystemManager::getSingleton()->getTerrain()->removeTerrain();

	//static_cast<TerrainSystem*>(GameSystemManager::getSingleton()->getSystem("TerrainSystem"))->removeTerrain();
	GameSystemManager::getSingleton()->getGUI()->showLoader(false);
	return true;
}
bool frmTerrain::evtTerrainRefresh_click(const CEGUI::EventArgs& e) {
	return true;
}
bool frmTerrain::evtTerrainGenerate_click(const CEGUI::EventArgs& e) {
	
	if(fillConfigFromGui()) { //if there was an error collecting the data dont generate the terrain
		GameSystemManager::getSingleton()->getGUI()->showLoader(true);
		
		GameSystemManager::getSingleton()->getTerrain()->generateTerrain();

		//static_cast<TerrainSystem*>(GameSystemManager::getSingleton()->getSystem("TerrainSystem"))->generateTerrain();
		GameSystemManager::getSingleton()->getGUI()->showLoader(false);
	}

	//GameSystemManager::getSingleton()->createTerrain();
	return true;
}


bool frmTerrain::evtTerrainList_click(const CEGUI::EventArgs& e) {

	std::string selected = GameSystemManager::getSingleton()->getGUI()->getSelectedListItem(e,"Terrain/List");

	CEGUI::Listbox* list = (CEGUI::Listbox*) CEGUI::WindowManager::getSingleton().getWindow("Terrain/List");
	CEGUI::ListboxTextItem* item =  (CEGUI::ListboxTextItem*) list->getFirstSelectedItem ();
	item->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");
	item->setSelected(true);

	CEGUI::Editbox *eb = (CEGUI::Editbox*)CEGUI::WindowManager::getSingleton().getWindow("Terrain/txtSelected");
	eb->setText(selected);
	
	GameSystemManager::getSingleton()->getGUI()->addImageToControl(selected,"Terrain/imgHeightMapPreview","heightmapImgset");
	//terrain_config.heightmapFileName = selected;
	//GameSystemManager::getSingleton()->getTerrain()->mHeightmapFileName = selected;
	terrain_config.mHeightmapFileName = selected;
	//GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("HeightmapFileName")->setValue(selected);

	//std::string t = GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("HeightmapFileName")->toString();
	//std::string t2 = *(static_cast<TerrainSystem*>(GameSystemManager::getSingleton()->getSystem("TerrainSystem"))->mHeightmapFileName);

	Ogre::Image img;
	img.load(selected,"ET");

	CEGUI::Window *lbl = (CEGUI::Window*)CEGUI::WindowManager::getSingleton().getWindow("Terrain/lblsize");
	Ogre::String size;
	//size = "Size: X" + StringConverter::toString(img.getWidth()) + " Y: " + StringConverter::toString(img.getHeight());
	size = "(" + StringConverter::toString(img.getWidth()) + " x " + StringConverter::toString(img.getHeight()) + ")";
	lbl->setText(size);

	terrain_config.mHeightmapWidth = img.getWidth();
	terrain_config.mHeightmapHeight = img.getHeight();
	
	//GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("HeightmapWidth")->setValue(StringConverter::toString(img.getWidth()));
	//GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("HeightmapHeight")->setValue(StringConverter::toString(img.getHeight()));

	return true;
}
bool frmTerrain::evtTerrainClose_click(const CEGUI::EventArgs& e) {
	CEGUI::Window* w = CEGUI::WindowManager::getSingleton().getWindow("Terrain");
	w->setVisible(false);
	return true;
}

bool frmTerrain::evtTerrainBrushSize_change(const CEGUI::EventArgs& e) {
	CEGUI::Slider *sl = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Terrain/brushTerrainSizeSlider");
	
	if(terrain_config.mIsTerrainLoaded)
		GameSystemManager::getSingleton()->getTerrain()->setTerrainEditBrush(sl->getCurrentValue());

	//static_cast<TerrainSystem*>(GameSystemManager::getSingleton()->getSystem("TerrainSystem"))->setTerrainEditBrush(sl->getCurrentValue());
	
	CEGUI::Window *lbl = (CEGUI::Window*)CEGUI::WindowManager::getSingleton().getWindow("Terrain/brushTerrainSizeValue");
	lbl->setText(StringConverter::toString(sl->getCurrentValue()));
	return true;
}
bool frmTerrain::evtTerrainBrushIntensity_change(const CEGUI::EventArgs& e) {
	CEGUI::Slider *sl = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Terrain/brushTerrainIntensitySlider");
	
	if(terrain_config.mIsTerrainLoaded)
		GameSystemManager::getSingleton()->getTerrain()->setTerrainEditBrush(sl->getCurrentValue());
	
	//static_cast<TerrainSystem*>(GameSystemManager::getSingleton()->getSystem("TerrainSystem"))->setTerrainEditBrushIntensity(sl->getCurrentValue());
	
	CEGUI::Window *lbl = (CEGUI::Window*)CEGUI::WindowManager::getSingleton().getWindow("Terrain/brushTerrainIntensityValue");
	lbl->setText(StringConverter::toString(sl->getCurrentValue()));
	return true;
}

bool frmTerrain::evtTerrainPaintSize_change(const CEGUI::EventArgs& e) {
	CEGUI::Slider *sl = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Terrain/brushPaintSizeSlider");
	
	if(terrain_config.mIsTerrainLoaded)
		GameSystemManager::getSingleton()->getTerrain()->setTerrainEditBrush(sl->getCurrentValue());

	//static_cast<TerrainSystem*>(GameSystemManager::getSingleton()->getSystem("TerrainSystem"))->setPaintEditBrush(sl->getCurrentValue());
	
	CEGUI::Window *lbl = (CEGUI::Window*)CEGUI::WindowManager::getSingleton().getWindow("Terrain/brushPaintSizeValue");
	lbl->setText(StringConverter::toString(sl->getCurrentValue()));
	return true;
}
bool frmTerrain::evtTerrainPaintIntensity_change(const CEGUI::EventArgs& e) {
	CEGUI::Slider *sl = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Terrain/brushPaintIntensitySlider");
	
	if(terrain_config.mIsTerrainLoaded)
		GameSystemManager::getSingleton()->getTerrain()->setTerrainEditBrush(sl->getCurrentValue());

	//static_cast<TerrainSystem*>(GameSystemManager::getSingleton()->getSystem("TerrainSystem"))->setPaintEditBrushIntensity(sl->getCurrentValue());
	
	CEGUI::Window *lbl = (CEGUI::Window*)CEGUI::WindowManager::getSingleton().getWindow("Terrain/brushPaintIntensityValue");
	lbl->setText(StringConverter::toString(sl->getCurrentValue()));
	return true;
}

bool frmTerrain::evtTerrainTextureList_click(const CEGUI::EventArgs& e) {
	const CEGUI::WindowEventArgs& wEventArgs = static_cast<const CEGUI::WindowEventArgs&>(e);
    CEGUI::Listbox* list = static_cast<CEGUI::Listbox*>(wEventArgs.window);

	//list->getItemCount
	
		//CEGUI::ItemListbox::Iterator *iter; //=list->beginInitialisation
		
		//for(iter = list->begi .begin(); iter != mTemplates.end(); iter++) {
		//	delete iter->second;
		//}
		//mTemplates.clear();

    const CEGUI::String Name = list->getName ();
    size_t selCount = list->getSelectedCount ();
    if (selCount == 1)
    {
		CEGUI::ListboxTextItem* item =  (CEGUI::ListboxTextItem*) list->getFirstSelectedItem ();
		const unsigned int pos = StringConverter::parseUnsignedInt (item->getText().c_str ());   
		//item->setTextColours(CEGUI::colour(0.4,0.4,1.0));
		item->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");

		Ogre::String os = item->getText().c_str();
		//terrain_config.mChosenTexture = StringConverter::parseUnsignedInt(os.substr(9,1));
		terrain_config.mChosenTexture = StringConverter::parseInt(os.substr(9,1));
		//GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("ChosenTexture")->setValue(os.substr(9,1));

		const char *matName = item->getText().c_str();
		item->setSelected(true);

        CEGUI::WindowManager& wMgr = CEGUI::WindowManager::getSingleton();
        CEGUI::Listbox* matList  =  static_cast<CEGUI::Listbox*> (CEGUI::WindowManager::getSingleton().getWindow("Terrain/lstTextures"));
        //matList->clearAllSelections();
        CEGUI::ListboxTextItem* itemMat = (CEGUI::ListboxTextItem*) matList->findItemWithText(matName, 0);
        matList->setItemSelectState (itemMat, true);
		
		GameSystemManager::getSingleton()->getGUI()->addImageToControl(matName,"Terrain/imgTexturesPreview","TexturesImgset");
		//terrain_config.splatTextureName = matName;
		terrain_config.mSplatTextureName = matName;
		//GameSystemManager::getSingleton()->getSystem("TerrainSystem")->getParam("SplatTextureName")->setValue(matName);

		Ogre::Image img;
		img.load(matName,"ET");

		CEGUI::Window *lbl = (CEGUI::Window*)CEGUI::WindowManager::getSingleton().getWindow("Terrain/lblTextureSize");
		Ogre::String size;
		size = "(" + StringConverter::toString(img.getWidth()) + " x " + StringConverter::toString(img.getHeight()) + ")";
		lbl->setText(size);
	}
    
	return true;
}

bool frmTerrain::evtTerrainTexturesDiskList_click(const CEGUI::EventArgs& e) {
	const CEGUI::WindowEventArgs& wEventArgs = static_cast<const CEGUI::WindowEventArgs&>(e);
    CEGUI::Listbox* list = static_cast<CEGUI::Listbox*>(wEventArgs.window);
    const CEGUI::String Name = list->getName ();
    size_t selCount = list->getSelectedCount ();
    if (selCount == 1)
    {
		CEGUI::ListboxTextItem* item =  (CEGUI::ListboxTextItem*) list->getFirstSelectedItem ();
		const unsigned int pos = StringConverter::parseUnsignedInt (item->getText().c_str ());   
		//item->setTextColours(CEGUI::colour(0.4,0.4,1.0));
		item->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");
		//terrain_config.mChosenTexture = list->getItemIndex(item);

		const char *matName = item->getText().c_str();
		item->setSelected(true);

        CEGUI::WindowManager& wMgr = CEGUI::WindowManager::getSingleton();
        CEGUI::Listbox* matList  =  static_cast<CEGUI::Listbox*> (CEGUI::WindowManager::getSingleton().getWindow("Terrain/lstTexturesDisk"));
        //matList->clearAllSelections();
        CEGUI::ListboxTextItem* itemMat = (CEGUI::ListboxTextItem*) matList->findItemWithText(matName, 0);
        matList->setItemSelectState (itemMat, true);
		
		GameSystemManager::getSingleton()->getGUI()->addImageToControl(matName,"Terrain/imgTexturesPreview","TexturesImgset");
		//terrain_config.splatTextureName = matName;
		
		terrain_config.mSplatTextureName = matName;		
		//static_cast<TerrainSystem*>(GameSystemManager::getSingleton()->getSystem("TerrainSystem"))->getParam("SplatTextureName")->setValue(matName);

		Ogre::Image img;
		img.load(matName,"ET");

		CEGUI::Window *lbl = (CEGUI::Window*)CEGUI::WindowManager::getSingleton().getWindow("Terrain/lblTextureSize");
		Ogre::String size;
		size = "(" + StringConverter::toString(img.getWidth()) + " x " + StringConverter::toString(img.getHeight()) + ")";
		lbl->setText(size);
	}
	return true;
}

bool frmTerrain::evtTerrainRemoveFromPalette_click(const CEGUI::EventArgs& e) {
    CEGUI::Listbox* list = static_cast<CEGUI::Listbox*>(CEGUI::WindowManager::getSingleton().getWindow("Terrain/lstTextures"));
    const CEGUI::String Name = list->getName();
    size_t selCount = list->getSelectedCount();
	if (selCount == 1) {
		CEGUI::ListboxTextItem* item =  (CEGUI::ListboxTextItem*) list->getFirstSelectedItem();
		const char *fileName = item->getText().c_str();
		
		GameSystemManager::getSingleton()->getTerrain()->removeTexture((std::string)fileName);

		//static_cast<TerrainSystem*>(GameSystemManager::getSingleton()->getSystem("TerrainSystem"))->removeTexture((std::string)fileName);
		fillTexturesList();
	}
	return true;
}

bool frmTerrain::evtTerrainAddToPalette_click(const CEGUI::EventArgs& e) {
	
    CEGUI::Listbox* list = static_cast<CEGUI::Listbox*>(CEGUI::WindowManager::getSingleton().getWindow("Terrain/lstTexturesDisk"));
    const CEGUI::String Name = list->getName ();
    size_t selCount = list->getSelectedCount ();
	if (selCount == 1) {
		CEGUI::ListboxTextItem* item =  (CEGUI::ListboxTextItem*) list->getFirstSelectedItem();
		const char *fileName = item->getText().c_str();
		
		GameSystemManager::getSingleton()->getTerrain()->addTexture((std::string)fileName);

		//static_cast<TerrainSystem*>(GameSystemManager::getSingleton()->getSystem("TerrainSystem"))->addTexture((std::string)fileName);
		fillTexturesList();
	}
	return true;
}

